<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="fetchData()"
          type="primary"
          size="small">
          查询结果
        </el-button>
        <el-button
          style="float: right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
      <div style="margin-top: 25px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="元器件名称：">
            <el-input style="width: 203px" v-model="listQuery.name" placeholder="元器件名称"></el-input>
          </el-form-item>
          <el-form-item label="SN：">
            <el-input style="width: 203px" v-model="listQuery.sn" placeholder="SN"></el-input>
          </el-form-item>
          <el-form-item label="项目：">
            <el-input style="width: 203px" v-model="listQuery.project" placeholder="项目"></el-input>
          </el-form-item>
          <el-form-item label="状态：">
            <el-select v-model="listQuery.status" placeholder="全部" clearable>
              <el-option v-for="item in electronicStatusOptions"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="类型：">
            <el-select v-model="listQuery.type" placeholder="全部" clearable>
              <el-option v-for="item in logTypeOptions"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <el-table
      :data="list"
      v-loading="listLoading"
      style="width: 100%; margin-top:20px">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="SN">
              <span>{{ props.row.electronic.sn }}</span>
            </el-form-item >
            <el-form-item label="名称">
              <span>{{ props.row.electronic.name }}</span>
            </el-form-item >
            <el-form-item label="封装">
              <span>{{ props.row.electronic.package }}</span>
            </el-form-item >
            <el-form-item label="价格">
              <span>{{ props.row.electronic.price }}</span>
              <span>{{ props.row.electronic.spec }}</span>
            </el-form-item >
            <el-form-item label="库存">
              <span>{{ props.row.electronic.stock }}</span>
            </el-form-item >
            <el-form-item label="位置">
              <span>{{ props.row.electronic.position }}</span>
            </el-form-item >
            <el-form-item label="项目">
              <span>{{ props.row.electronic.project }}</span>
            </el-form-item >
            <el-form-item label="供货商">
              <span>{{ props.row.electronic.supplier }}</span>
            </el-form-item >
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="记录类型" prop="name">
        <template slot-scope="scope">{{scope.row.log.type | formatLogType}}</template>
      </el-table-column>
      <el-table-column label="记录数量" prop="name">
        <template slot-scope="scope">{{scope.row.log.count}}</template>
      </el-table-column>
      <el-table-column label="名称" prop="name">
        <template slot-scope="scope">{{scope.row.electronic.name}}</template>
      </el-table-column>
      <el-table-column label="价格/规格" prop="stock">
        <template slot-scope="scope">
          <p>{{scope.row.electronic.price}} / {{scope.row.electronic.spec}}</p>
        </template>
      </el-table-column>
      <el-table-column label="位置" prop="position">
      </el-table-column>
      <el-table-column label="项目/产品" prop="project">
        <template slot-scope="scope">{{scope.row.electronic.project}}</template>
      </el-table-column>
      <el-table-column label="供货商" prop="project">
        <template slot-scope="scope">{{scope.row.electronic.supplier}}</template>
      </el-table-column>
      <el-table-column label="状态" prop="project">
        <template slot-scope="scope">{{scope.row.electronic.status | formatStatus}}</template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData" />

  </div>
</template>

<script>

import {
  fetchElectronicLog
} from '@/api/electronicLog'

import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

const defaultListQuery = {
  name: null,
  project: null,
  sn: null,
  status: null,
  type: null,
  page: 1,
  limit: 20
};
export default {
  name: 'find',
  components: { Pagination },
  directives: { waves },
  filters: {
    formatCreateTime(time) {
      let date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    },
    formatStatus(value) {
      if (value === 1) {
        return '正常';
      } else if (value === 2) {
        return '已删除';
      }
    },
    formatLogType(value) {
      if (value === 1) {
        return '入库';
      } else if (value === 2) {
        return '出库';
      }
    },
  },
  data() {
    return {
      formLabelWidth: '120px',
      tableKey: 0,
      list: null,
      listLoading: true,
      total: 10,
      selectRow: null,
      listQuery: Object.assign({}, defaultListQuery),
      electronicStatus: '',
      logType: '',
      form: {
        sn: null,
        name: null,
        package: null,
        spec: null,
        price: null,
        stock: null,
        delivery: null,
        position: null,
        supplier: null,
        project: null
      },
      electronicStatusOptions: [
        {
          label: '正常',
          value: 1
        },
        {
          label: '已删除',
          value: 2
        }
      ],
      logTypeOptions: [
        {
          label: '入库',
          value: 1
        },
        {
          label: '出库',
          value: 2
        }
      ]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      fetchElectronicLog(this.listQuery).then(response => {
        this.listLoading = false
        this.list = response.data.items
        this.total = response.data.total
      })
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },
    handleEdit(row) {
      this.$router.push({
        path: '/sdc/electronicUpdate',
        query: row
      })
    },
    handleAdd(row) {
      this.$router.push({
        path: '/sdc/electronicAdd',
        query: row
      })
    },
    handleDelete(row) {
      electronicDelete(row.electronic.id).then(response => {
        this.$message(response.message)
      })
    },
    handleBoomConfirm(electronicBoomForm) {
      this.$refs[electronicBoomForm].validate((valid) => {
        if (valid) {
          console.log(this.electronicBoomForm)
          addElectronicBoomItem(this.electronicBoomForm).then(response => {
            this.$message(response.message)
            this.dialogElectronicBoomVisiable = false
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleFetchallBoom() {
      fetchallElectronicBoom().then(response => {
        this.electronicBoomList = response.data.items
      })
    },
    handleJoinBoom(row) {
      this.dialogElectronicBoomVisiable = true
      this.electronicBoomForm.electronic.id = row.electronic.id
    }
  }
}
</script>

<style>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>

